/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode *head = new ListNode(0);
        ListNode *ret = head;
        ListNode *cur1 = l1, *cur2 = l2;
        int carry = 0; // 进位
        while(cur1 || cur2 || carry)
        {
            int val = carry;
            if(cur1)
            {
                val += cur1->val;
                cur1 = cur1->next;
            }
            if(cur2)
            {
                val += cur2->val;
                cur2 = cur2->next;
            }
            head->next = new ListNode(val % 10);
            head = head->next;
            carry =  val / 10;
        }
        return ret->next;
    }
};